require_relative 'sample_pdu'
require 'test/unit'
require 'lib/smppclient/pdu/pdu_factory'
require 'lib/smppclient/util/byte_buffer'
require 'lib/smppclient/pdu/command_id'

class TestPduFactory < MiniTest::Unit::TestCase

  include SmppClient::Util
  include SmppClient::Pdu

  def test_parse
    bb = ByteBuffer.new
    bb.append_int(16)
    bb.append_int(0x00000015)
    bb.append_int(0)
    bb.append_int(1000)

    pdu = PduFactory.parse(bb)
    assert_equal(16, pdu.command_length)
    assert_equal(CommandId::ENQUIRE_LINK, pdu.command_id)
    assert_equal(CommandStatus::ESME_ROK, pdu.command_status)
    assert_equal(1000, pdu.sequence_number)
    assert_equal('000000100000001500000000000003e8', pdu.buffer.to_s)
  end

  def test_parse_tag_not_found
    bb = ByteBuffer.new
    bb.append_int(16)
    bb.append_int(0x99999999)
    bb.append_int(0)
    bb.append_int(1000)
    assert_raises(PduError) { PduFactory.parse(bb) }
  end

  def test_add_remove
    bb = ByteBuffer.new
    bb.append_int(16)
    bb.append_int(SamplePdu::COMMAND_ID)
    bb.append_int(0)
    bb.append_int(1000)

    PduFactory.add(SamplePdu::COMMAND_NAME, SamplePdu::COMMAND_ID, SamplePdu)

    pdu = PduFactory.parse(bb)
    assert_equal(16, pdu.command_length)
    assert_equal(SamplePdu::COMMAND_ID, pdu.command_id)
    assert_equal(CommandStatus::ESME_ROK, pdu.command_status)
    assert_equal(1000, pdu.sequence_number)
    assert_equal('000000101111111100000000000003e8', pdu.buffer.to_s)

    PduFactory.remove(SamplePdu::COMMAND_ID)
  end

  def test_add_same_tag_twice
    assert_raises(PduError) {
      PduFactory.add('SamplePdu', CommandId::ALERT_NOTIFICATION, SamplePdu)
    }
  end

  def test_remove_not_existing_tag
    PduFactory.remove(0x88888888)
  end

end
